//
//  DR_NativeView.swift
//  DocReaderRP
//
//  Created by  mac on 2025/11/19.
//

import UIKit
import GoogleMobileAds

class DR_NativeView: UIView, NativeAdDelegate {

    var adSense = ""
    fileprivate var v_native: NativeAdView?
    var nativeAd: NativeAd? {
        didSet {
            v_native?.mediaView?.mediaContent = nativeAd?.mediaContent
            (v_native?.bodyView as? UILabel)?.text = nativeAd?.body
            (v_native?.iconView as? UIImageView)?.image = nativeAd?.icon?.image
            (v_native?.headlineView as? UILabel)?.text = nativeAd?.headline
            (v_native?.callToActionView as? UIButton)?.setTitle(nativeAd?.callToAction, for: .normal)
            v_native?.callToActionView?.isUserInteractionEnabled = false
            v_native?.nativeAd = nativeAd
            nativeAd?.delegate = self
        }
    }
    
    init() {
        super.init(frame: .zero)
        
        var nibName = "DR_NativeBuyView"
//        if let configModel = DR_RemoteManager.shared.adConfigModel, configModel.usermodel == "1" {
//            
//        }
        guard
          let nibObjects = Bundle.main.loadNibNamed(nibName, owner: nil, options: nil),
          let adView = nibObjects.first as? NativeAdView
        else {
          assert(false, "Could not load nib file for adView")
            return
        }
        addSubview(adView)
        adView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        self.v_native = adView
        adView.backgroundColor = .clear
    }
    
    // MARK: - 广告视图点击事件
    func nativeAdDidRecordClick(_ nativeAd: NativeAd) {
        print("🔘 原生广告点击事件被记录")
        // 使用 ad_scense 事件，避免与 Firebase 保留的 ad_click 冲突
//        RaeAnalytics.logEvent(.ad_scense, ["sceneName": adSense])
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
